package edu.school.controller;

import edu.school.entity.Admin;
import edu.school.entity.User;
import edu.school.mapper.UserMapper;
import edu.school.service.AdminService;
import edu.school.service.UserService;
import edu.school.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author kabuda
 * @create 2023-09-27 18:06
 */
@Controller
public class LoginController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "toRegister",method = RequestMethod.GET)
    public String toRegister(){
        return "register";
    }
    @RequestMapping(value = "toLogin",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }
    @RequestMapping(value = "toMain",method = RequestMethod.GET)
    public String toMain(){
        return "main";
    }
    @RequestMapping(value = "toConsole",method = RequestMethod.GET)
    public String toConsole(){
        return "console";
    }
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        //清楚session数据
        session.invalidate();
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(User user){

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        user.setCreateTime(timestamp);
        if(userService.register(user)){
            return "<script>alert('success');location.href='/toLogin'</script>";
        }else{
            return "<script>alert('fail');history.to(-1)</script>";
        }
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(LoginVo loginVo, HttpSession session, Model model) {
        int type = loginVo.getType();
        if(type==1){
            User user = new User();
            user.setStuno(loginVo.getUsername());
            user.setPwd(loginVo.getPwd());
            User u=userService.login(user);
            if (u != null) {
                session.setAttribute("user", u);
                session.setAttribute("type", 1);
                return "redirect:toMain";
            }
        }else if(type==2){
            Admin admin = new Admin();
            admin.setUsername(loginVo.getUsername());
            admin.setPwd(loginVo.getPwd());
            Admin a = adminService.login(admin);
            if (a != null) {
                session.setAttribute("admin", a);
                session.setAttribute("type", 2);
                return "redirect:toMain";
            }
        }else{
            throw new RuntimeException("类型错误");
        }

        model.addAttribute("msg","账号或密码错误，请重新输入");
        return "login";
    }
}
